package com.emily.test.controller;

import com.alibaba.fastjson.JSONObject;
import com.emily.test.controller.result.ResponseResult;
import com.emily.test.model.TCommonWeb;
import com.emily.test.service.RedisService;
import com.emily.test.service.TCommonWebService;
import com.emily.test.utils.ConstentUtil;
import com.emily.test.utils.HeaderUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @author ：zc
 * @date ：Created in 2019/4/16 11:12
 * @description：
 */
@Slf4j
@RestController
@CrossOrigin(origins = "*", maxAge = 3600)
@RequestMapping(value = "commonWeb")
@Api(value = "公司常用网站", description = "公司常用网站接口", position = 1, produces = MediaType.APPLICATION_JSON_VALUE)
public class TCommonWebController {
    @Autowired
    TCommonWebService tCommonWebService;
    @Autowired
    RedisService redisService;

    @ApiOperation(value = "获取公司下的网站列表", notes = "获取公司下的网站列表", httpMethod = "POST")
    @PostMapping(value = "getList")
    public ResponseResult getList(HttpServletRequest request) {
        try {
            String code = HeaderUtil.getCompanyCode(request, redisService);
            return ResponseResult.success("0", "查询成功", tCommonWebService.getCommomWebByCode(code));
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseResult.fail("查询失败");
        }
    }

    @ApiOperation(value = "增加网站", notes = "增加公司常用网站", httpMethod = "POST")
    @PostMapping(value = "intoWeb")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "网站名称", required = true),
            @ApiImplicitParam(name = "userName", value = "登陆名", required = true),
            @ApiImplicitParam(name = "password", value = "登陆密码", required = true),
            @ApiImplicitParam(name = "remark", value = "备注", required = false),
            @ApiImplicitParam(name = "url", value = "网站地址", required = true),
    })
    public ResponseResult intoWeb(@RequestBody JSONObject jsonObject, HttpServletRequest request) {
        log.info("添加的网站信息为：{}", jsonObject.toString());
        try {
            String code = HeaderUtil.getCompanyCode(request, redisService);
            TCommonWeb tCommonWeb = new TCommonWeb();
            tCommonWeb.setCompanyCode(code);
            tCommonWeb.setName(jsonObject.getString("name"));
            tCommonWeb.setUserName(jsonObject.getString("userName"));
            tCommonWeb.setPassword(jsonObject.getString("password"));
            tCommonWeb.setRemark(jsonObject.getString("remark"));
            tCommonWeb.setUrl(jsonObject.getString("url"));
            return ResponseResult.success("0", "插入成功", tCommonWebService.insert(tCommonWeb));
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseResult.fail("查询失败");
        }
    }
    @ApiOperation(value = "修改网站", notes = "修改公司常用网站", httpMethod = "POST")
    @PostMapping(value = "updateWeb")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "wid", value = "id", required = true),
            @ApiImplicitParam(name = "name", value = "网站名称", required = true),
            @ApiImplicitParam(name = "userName", value = "登陆名", required = true),
            @ApiImplicitParam(name = "password", value = "登陆密码", required = true),
            @ApiImplicitParam(name = "remark", value = "备注", required = false),
            @ApiImplicitParam(name = "url", value = "网站地址", required = true),
    })
    public ResponseResult updateWeb(@RequestBody JSONObject jsonObject) {
        log.info("修改的网站信息为：{}", jsonObject.toString());
        try {
            TCommonWeb tCommonWeb = tCommonWebService.getCommomWebById(jsonObject.getInteger("wid"));
            tCommonWeb.setName(jsonObject.getString("name"));
            tCommonWeb.setUrl(jsonObject.getString("url"));
            tCommonWeb.setUserName(jsonObject.getString("userName"));
            tCommonWeb.setPassword(jsonObject.getString("password"));
            tCommonWeb.setRemark(jsonObject.getString("remark"));
            return ResponseResult.success("0", "修改成功", tCommonWebService.update(tCommonWeb));
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseResult.fail("修改失败");
        }
    }
    @ApiOperation(value = "删除网站", notes = "删除公司常用网站", httpMethod = "POST")
    @PostMapping(value = "delWeb")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", value = "id", required = true),
    })
    public ResponseResult delWeb(@RequestBody JSONObject jsonObject) {
        log.info("删除网站信息为：{}", jsonObject.toString());
        try {
            String ids=jsonObject.getString("ids").replaceAll("\\[","").replaceAll("]","");
            String[] idStr=ids.split(",");
            if(idStr.length !=0){
                for( int i=0;i<idStr.length;i++){
                    tCommonWebService.deleteCommonWeb(ConstentUtil.strToInteger(idStr[i]));
                }
            }

            return ResponseResult.success("删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseResult.fail("删除失败");
        }
    }
    @ApiOperation(value = "搜索网站信息", notes = "通过网站名字搜索公司常用网站", httpMethod = "POST")
    @PostMapping(value = "getWebByName")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "网站名称", required = true),
    })
    public ResponseResult getWebByName(@RequestBody JSONObject jsonObject) {
        log.info("查询的网站信息为：{}", jsonObject.toString());
        try {
            return ResponseResult.success("0", "查询成功",tCommonWebService.getWebByName(jsonObject.getString("name")));
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseResult.fail("查询失败");
        }
    }
    @ApiOperation(value = "通过id获取网站信息", notes = "通过id获取网站信息", httpMethod = "POST")
    @PostMapping(value = "getWebById")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "wid", value = "id", required = true),
    })
    public ResponseResult getWebById(@RequestBody JSONObject jsonObject) {
        log.info("查询的网站信息为：{}", jsonObject.toString());
        try {
            return ResponseResult.success("0", "查询成功",tCommonWebService.getCommomWebById(jsonObject.getInteger("wid")));
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseResult.fail("查询失败");
        }
    }
}
